package cn.com.efuture.o2o.backend.mybatis.service;

import cn.com.efuture.o2o.backend.mybatis.entity.ExportJob;
import cn.com.efuture.o2o.backend.mybatis.entity.GenFile;
import cn.com.efuture.o2o.backend.mybatis.entity.ImportJob;
import cn.com.efuture.o2o.backend.mybatis.mapper.FileMapper;
import cn.com.efuture.o2o.backend.mybatis.mapper.FileMongoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class FileServiceImpl {

    private final FileMapper fileMapper;

    private final FileMongoMapper fileMongoMapper;

    public FileServiceImpl(FileMapper fileMapper, FileMongoMapper fileMongoMapper) {
        this.fileMapper = fileMapper;
        this.fileMongoMapper = fileMongoMapper;
    }

    @Transactional
    public void insertImportJob(GenFile genFile, String userName) {
        //存入mongoDB
        fileMongoMapper.save(genFile);
        //插入ImportJob
        ImportJob importJob = new ImportJob();
        importJob.setOperator(userName);
        importJob.setFileId(genFile.getFileId());
        importJob.setFileName(genFile.getFileName());
        importJob.setType(genFile.getType());
        if ("ImportProduct".equals(genFile.getType())) {
            importJob.setTypeName("导入商品");
        } else if ("ImportGoods".equals(genFile.getType())) {
            importJob.setTypeName("导入门店商品");
        } else if ("ImportImage".equals(genFile.getType())) {
            importJob.setTypeName("导入图片");
        } else if ("ImportO2oGoodsFilter".equals(genFile.getType())) {
            importJob.setTypeName("导入主档商品筛选");
        }
        fileMapper.insertImportJob(importJob);
    }

    public GenFile fileDownloadById(String id) {
        return fileMongoMapper.findOne(id);
    }

    public List<ImportJob> getImportJobList(Map<String, Object> map) {
        return fileMapper.getImportJobList(map);
    }

    public List<ExportJob> getExportJobList(Map<String, Object> map) {
        return fileMapper.getExportJobList(map);
    }

    public void insertExportJob(ExportJob exportJob, String userName) {
        exportJob.setOperator(userName);
        exportJob.setStatus(0);
        if ("ExportProduct".equals(exportJob.getType())) {
            exportJob.setTypeName("导出商品");
        }else if ("ExportGoods".equals(exportJob.getType())) {
            exportJob.setTypeName("导出门店商品");
        }else if ("ExportGoodsLog".equals(exportJob.getType())) {
            exportJob.setTypeName("导出商品操作日志");
        }else if ("ExportO2oInventoryLog".equals(exportJob.getType())) {
            exportJob.setTypeName("导出商品库存日志");
        }else if ("ExportChannelCategoryLog".equals(exportJob.getType())) {
            exportJob.setTypeName("导出类别日志");
        }else if ("ExportChannelShopLog".equals(exportJob.getType())) {
            exportJob.setTypeName("导出门店日志");
        }else if ("ExportO2oGoodsFilter".equals(exportJob.getType())) {
            exportJob.setTypeName("导出主档商品筛选");
        }
        fileMapper.insertExportJob(exportJob);
    }
}
